package com.blog4j.user.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.blog4j.common.annotation.OperationLog;
import com.blog4j.common.enums.ModuleTypeEnum;
import com.blog4j.common.enums.OperationTypeEnum;
import com.blog4j.common.model.Result;
import com.blog4j.requestRate.annotation.RequestRate;
import com.blog4j.user.entity.MessageEntity;
import com.blog4j.user.service.MessageService;
import com.blog4j.user.vo.req.DelMessageReqVo;
import com.blog4j.user.vo.req.MessageListReqVo;
import com.blog4j.user.vo.resp.MessageRespVo;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author 98k灬
 * @version v1.0.0
 * @Description : 功能描述
 * @Create on : 2024/8/15 12:16
 **/
@RequestMapping("/message")
@RestController
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class MessageController {
    private final MessageService messageService;

    /**
     * 检查用户是否有新消息
     *
     * @param userId 用户ID
     * @return 是否有新消息
     */
    @RequestRate(count = 5)
    @OperationLog(module= ModuleTypeEnum.USER, description = "检查用户是否有新消息")
    @SaCheckLogin
    @GetMapping("/userHasNewMessage")
    public Result userHasNewMessage(@RequestParam("userId") String userId) {
        Integer count = messageService.userHasNewMessage(userId);
        return Result.ok(count);
    }

    /**
     * 获取消息列表
     *
     * @param reqVo 请求信息
     * @return 消息列表
     */
    @RequestRate
    @OperationLog(module= ModuleTypeEnum.USER, description = "获取消息列表")
    @SaCheckLogin
    @PostMapping("/messageList")
    public Result messageList(@RequestBody @Valid MessageListReqVo reqVo) {
        PageInfo<MessageRespVo> pageInfo = messageService.messageList(reqVo);
        return Result.ok(pageInfo);
    }

    /**
     * 删除消息
     *
     * @param reqVo 请求信息
     * @return 删除成功
     */
    @RequestRate(count = 5)
    @OperationLog(operationType= OperationTypeEnum.DELETE,
            module= ModuleTypeEnum.USER,
            description = "删除消息")
    @SaCheckLogin
    @PostMapping("/deleteMessage")
    public Result deleteMessage(@RequestBody @Valid DelMessageReqVo reqVo) {
        messageService.deleteMessage(reqVo);
        return Result.ok();
    }

    /**
     * 设置全部消息已读
     *
     * @return 成功
     */
    @RequestRate(count = 5)
    @OperationLog(operationType= OperationTypeEnum.EDIT,
            module= ModuleTypeEnum.USER,
            description = "设置全部消息已读")
    @SaCheckLogin
    @GetMapping("/allReadMessage")
    public Result allReadMessage() {
        messageService.allReadMessage();
        return Result.ok();
    }

    /**
     * 删除全部消息
     *
     * @return 成功
     */
    @RequestRate(count = 5)
    @OperationLog(operationType= OperationTypeEnum.DELETE,
            module= ModuleTypeEnum.USER,
            description = "删除全部消息")
    @SaCheckLogin
    @GetMapping("/deleteAllMessage")
    public Result deleteAllMessage() {
        messageService.deleteAllMessage();
        return Result.ok();
    }
}
